<template>
  <div>
    <r-search :searchHandle="searchHandleAdd" />
    <r-search
      :searchForm="searchForm"
      :searchHandle="searchHandle"
      :searchData="searchData"
    />
    <r-table
      :isSelection="true"
      :isIndex="true"
      :isPagination="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      @refresh="init()"
    ></r-table>
    <r-form
      :formRules="formRules"
      :formHandle="formHandle"
      :formData="formData"
      :formCols="formCols"
      ref="elForm"
      labelWidth="200px"
      dialogWidth="1000px"
      :isHandle="true"
    />
  </div>
</template>
<script>
export default {
  data() {
    var checkpass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("Enter Again PassWord"));
      } else if (value !== this.formDataEditPsw.password) {
        callback(new Error("The password is not the same!"));
      } else {
        callback();
      }
    };
    return {
      searchData: {
        activeName: "",
        startTime: "",
        endTime: "",
        status: ""
      },
      searchForm: [
        {
          type: "input",
          prop: "activeName",
          placeholder: "活动Name",
          width: 170
        },
        {
          type: "date",
          prop: "startTime",
          placeholder: "活动Start Time",
          width: 190
        },
        {
          type: "date",
          prop: "endTime",
          placeholder: "活动结束时间",
          width: 190
        },
        {
          type: "select",
          prop: "status",
          placeholder: "Status",
          width: 150,
          options: [
            {
              label: "上架",
              value: "1"
            },
            {
              label: "下架",
              value: "2"
            }
          ]
        }
      ],
      searchHandleAdd: [
        {
          label: "Add活动",
          type: "success",
          icon: "el-icon-circle-plus-outline",
          handle: e => this.elFormVisible()
        }
      ],
      searchHandle: [
        {
          label: "Search",
          type: "success",
          icon: "el-icon-search",
          handle: e => this.init()
        },
        {
          label: "Reset",
          icon: "el-icon-refresh-right",
          handle: e => this.searchFromReset()
        }
      ],
      tableData: [],
      tableCols: [
        {
          label: "活动Name",
          prop: "activeName"
          // theme: e => e.status ? 'primary' : 'danger', type: "tag",
        },
        {
          label: "活动周期Start Time",
          prop: "startTime",
          width: 100,
          formatter: e => e.startTime.split(" ")[0]
        },
        {
          label: "活动周期结束时间",
          prop: "endTime",
          formatter: e => e.endTime.split(" ")[0],
          width: 100
        },
        {
          label: "设备数量",
          prop: "equipmentCount"
        },
        {
          label: "商品数量",
          prop: "goodsCount"
        },
        {
          label: "Status",
          prop: "status",
          theme: e => (e.status == "2" ? "danger" : "primary"),
          formatter: e => (e.status == "2" ? "下架" : "上架"),
          type: "tag"
        },
        {
          label: "Creation Time",
          prop: "ctime",
          width: 160,
          formatter: e =>
            e.ctime && this.getDeta(+new Date(e.ctime.replace(/T/g, " ")))
        },
        {
          label: "Operations",
          type: "button",
          width: 400,
          btnList: [
            {
              label: "Edit",
              type: "primary",
              size: "mini",
              // isShow: e => e.orderId == '123' ? true : false,
              handle: row => this.elFormDetail(row)
            },
            {
              label: "Delete",
              type: "primary",
              size: "mini",
              handle: row => this.TableDel("/active", { id: row.id }, "del")
            },
            {
              label: "商品管理",
              type: "primary",
              size: "mini",
              handle: row =>
                this.$router.push({
                  path: "/discount-goodsMg",
                  query: { id: row.id }
                })
            },
            {
              label: "Equipment Management",
              type: "primary",
              size: "mini",
              handle: row =>
                this.$router.push({
                  path: "/discount-equipmentMg",
                  query: { id: row.id }
                })
            }
          ]
        }
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
      formData: {
        activeName: "",
        startTime: "",
        endTime: "",
        status: ""
      },
      formCols: [
        {
          type: "input",
          label: "活动Name",
          placeholder: "Please Input活动Name",
          width: 350,
          prop: "activeName"
        },
        {
          type: "datet",
          label: "活动周期Start Time",
          placeholder: "Please select the start time",
          width: 350,
          prop: "startTime"
        },
        {
          type: "datet",
          label: "活动周期结束时间",
          placeholder: "Please select the end time",
          width: 350,
          prop: "endTime"
        },
        {
          type: "select",
          label: "Status",
          placeholder: "Please SelectStatus",
          width: 350,
          prop: "status",
          options: [
            { label: "Enable", value: 1 },
            { label: "Forbidden", value: 2 }
          ]
        }
      ],
      formRules: {
        activeName: [
          { required: true, message: "Please Input活动Name", trigger: "blur" }
        ],
        startTime: [
          {
            required: true,
            message: "Please select the start time",
            trigger: "blur"
          }
        ],
        endTime: [
          {
            required: true,
            message: "Please select the end time",
            trigger: "blur"
          }
        ],
        status: [{ required: true, message: "Status", trigger: "blur" }]
      },
      formHandle: [
        {
          label: "Cancel",
          handle: e => this.elFormVisible()
        },
        {
          label: "Confirm",
          type: "success",
          handle: e => this.elFormSubmit()
        }
      ]
    };
  },
  components: {},
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.tablePage.offset = this.tablePage.page;
      let param = { ...this.searchData, ...this.tablePage };
      // console.log(param);
      let res = await this.api.activelist(param);
      if (res) {
        this.tablePage.total = res.data.total;
        this.tableData = res.data.list;
        // this.tableData.map(item => {
        // item.startTime = this.getDeta(+new Date(item.startTime.replace(/T/g, ' ')))
        // item.endTime = this.getDeta(+new Date(item.startTime.replace(/T/g, ' ')))
        // })
      }
    },
    async elFormSubmit() {
      this.formData.account = this.formData.phone;
      if (this.formData.id) {
        let res = await this.api.activeupdate(this.formData);
        if (res) {
          this.formData.id = "";
          this.elFormVisible();
          this.init();
        }
      } else {
        let res = await this.api.activeadd(this.formData);
        if (res) {
          this.elFormVisible();
          this.init();
        }
      }
    },
    elFormDetail(row) {
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = row[key];
      });
      this.formData.id = row.id;
      this.formData.password = "";
      this.elFormVisible();
    }
  }
};
</script>
<style scoped lang="scss">
.el-icon-search {
  margin-right: 10px;
}
.Tip {
  margin-left: 20px;
}
</style>
